home *** CD-ROM | disk | FTP | other *** search
/ io Programmo 60 / IOPROG_60.ISO / soft / c++ / gsl-1.1.1-setup.exe / {app} / src / randist / bernoulli.c next >
Encoding:
C/C++ Source or Header  |  2001-05-14  |  1.4 KB  |  62 lines

  1. /* randist/bernoulli.c
  2.  * 
  3.  * Copyright (C) 1996, 1997, 1998, 1999, 2000 James Theiler, Brian Gough
  4.  * 
  5.  * This program is free software; you can redistribute it and/or modify
  6.  * it under the terms of the GNU General Public License as published by
  7.  * the Free Software Foundation; either version 2 of the License, or (at
  8.  * your option) any later version.
  9.  * 
  10.  * This program is distributed in the hope that it will be useful, but
  11.  * WITHOUT ANY WARRANTY; without even the implied warranty of
  12.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  13.  * General Public License for more details.
  14.  * 
  15.  * You should have received a copy of the GNU General Public License
  16.  * along with this program; if not, write to the Free Software
  17.  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  18.  */
  19.  
  20. #include <config.h>
  21. #include <math.h>
  22. #include <gsl/gsl_rng.h>
  23. #include <gsl/gsl_randist.h>
  24.  
  25. /* The bernoulli distribution has the form,
  26.  
  27.    prob(0) = 1-p, prob(1) = p
  28.  
  29.    */
  30.  
  31. unsigned int
  32. gsl_ran_bernoulli (const gsl_rng * r, double p)
  33. {
  34.   double u = gsl_rng_uniform (r) ;
  35.  
  36.   if (u < p)
  37.     {
  38.       return 1 ;
  39.     }
  40.   else
  41.     {
  42.       return 0 ;
  43.     }
  44. }
  45.  
  46. double
  47. gsl_ran_bernoulli_pdf (const unsigned int k, double p)
  48. {
  49.   if (k == 0)
  50.     {
  51.       return 1 - p ;
  52.     }
  53.   else if (k == 1)
  54.     {
  55.       return p ;
  56.     }
  57.   else
  58.     {
  59.       return 0 ;
  60.     }
  61. }
  62.